package com.csdn.leon.adapter;

import java.util.ArrayList;
import java.util.List;

import com.csdn.leon.model.BlogModel;
import com.csdn.leon.util.Constants;
import com.csdn.leon.util.FileUtil;
import com.csdn.leon.util.MyTagHandler;
import com.csdn.leon.util.SPUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.csdn.leon.base.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BlogDetailAdapter extends BaseAdapter {
	private ViewHolder holder;
	private LayoutInflater layoutInflater;
	private Context context;
	private List<BlogModel> list;
	private DisplayImageOptions options;

	public BlogDetailAdapter(Context context) {
		this.context = context;
		list = new ArrayList<BlogModel>();
		layoutInflater = layoutInflater.from(context);
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.csdn)
				.showImageForEmptyUri(R.drawable.csdn)
				.showImageOnFail(R.drawable.csdn).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true).build();
	}

	public void setList(List<BlogModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void addList(List<BlogModel> list) {
		this.list.addAll(list);
	}

	public void clearList() {
		this.list.clear();
	}

	public List<BlogModel> getList() {
		return list;
	}

	public void removeItem(int position) {
		if (list.size() > 0) {
			list.remove(position);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BlogModel blog = list.get(position);

		if (null == convertView) {
			holder = new ViewHolder();

			switch (blog.getState()) {
			case Constants.DEF_BLOG_ITEM_TYPE.TITLE:
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_title_item, null);
				holder.content = (TextView) convertView
						.findViewById(R.id.title_text);
				holder.date = (TextView) convertView
						.findViewById(R.id.title_time);
				break;

			case Constants.DEF_BLOG_ITEM_TYPE.SUMMARY:
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_summry_item, null);
				holder.content = (TextView) convertView
						.findViewById(R.id.summry_text);
				break;
			case Constants.DEF_BLOG_ITEM_TYPE.CONTENT:
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_content_item, null);
				holder.content = (TextView) convertView
						.findViewById(R.id.content_text);
				break;

			case Constants.DEF_BLOG_ITEM_TYPE.IMG: // 图片
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_image_item, null);
				holder.image = (ImageView) convertView
						.findViewById(R.id.imageView);
				break;
			case Constants.DEF_BLOG_ITEM_TYPE.BOLD_TITLE: // 加粗标题
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_blod_item, null);
				holder.content = (TextView) convertView
						.findViewById(R.id.blod_text);
				break;
			case Constants.DEF_BLOG_ITEM_TYPE.CODE: // 代码
				convertView = layoutInflater.inflate(
						R.layout.blog_detail_code_item, null);
				holder.code = (WebView) convertView
						.findViewById(R.id.code_view);
				break;
			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (null != blog) {
			switch (blog.getState()) {
			case Constants.DEF_BLOG_ITEM_TYPE.TITLE:
				System.out.println("文章标题：" + blog.getContent());
				holder.content.setText(Html.fromHtml(blog.getContent(), null,
						new MyTagHandler()));
				holder.date.setText(blog.getDate());

				break;
			case Constants.DEF_BLOG_ITEM_TYPE.IMG:
				// holder.image.setImageBitmap(BitmapUtil.getBitmap(blog.getContent(),
				// context));
				holder.image.setMinimumWidth(120);
				holder.image.setMinimumHeight(120);
				if ((boolean) SPUtils.get(context, Constants.KEY_LOAD_IMAGE,
						true)) {
					ImageLoader.getInstance().displayImage(blog.getImgLink(),
							holder.image, options);
				}

				break;
			case Constants.DEF_BLOG_ITEM_TYPE.CODE: // 代码，格式显示

				// 读取代码文件和模板文件
				String code = blog.getContent();
				// String code = FileUtil.getFileContent(context,
				// "AboutActivity.java");
				String template = FileUtil.getFileContent(context, "code.html");
				// 生成结果
				String html = template.replace("{{code}}", code);
				holder.code.getSettings().setDefaultTextEncodingName("utf-8");
				holder.code.getSettings().setSupportZoom(true);
				holder.code.getSettings().setBuiltInZoomControls(true);

				// holder.code.loadUrl("file:///android_asset/code.html");

				holder.code.loadDataWithBaseURL("file:///android_asset/", html,
						"text/html", "utf-8", null);

				break;
			default:
				holder.content.setText(Html.fromHtml(blog.getContent(), null,
						new MyTagHandler()));
				break;
			}
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {
		return 6;
	}

	@Override
	public int getItemViewType(int position) {
		switch (list.get(position).getState()) {
		case Constants.DEF_BLOG_ITEM_TYPE.TITLE:
			return 0;
		case Constants.DEF_BLOG_ITEM_TYPE.SUMMARY:
			return 1;
		case Constants.DEF_BLOG_ITEM_TYPE.CONTENT:
			return 2;
		case Constants.DEF_BLOG_ITEM_TYPE.IMG:
			return 3;
		case Constants.DEF_BLOG_ITEM_TYPE.BOLD_TITLE:
			return 4;
		case Constants.DEF_BLOG_ITEM_TYPE.CODE:
			return 5;
		}
		return 1;
	}

	private class ViewHolder {
		TextView id;
		TextView date;
		TextView title;
		TextView content;
		ImageView image;
		WebView code;
	}

}
